local extension = Package("nshou_nx")
extension.extensionName = "tshu"

Fk:loadTranslationTable{
  ["nshou_nx"] = "年兽.年夕",
  ["tshunx"] = "年夕",
}

local U = require "packages/utility/utility"

local nwei = General(extension, "tshunx__nwei","wei",7)
nwei.hidden = true
local fange = fk.CreateTriggerSkill{
  name = "tshu__fange",
  anim_type = "defensive",
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = player:drawCards(2,self.name)
    if math.abs(Fk:getCardById(cards[1]).number - Fk:getCardById(cards[2]).number) >= player.hp and data.from 
    and ((player.role == "rebel" and data.from.role ~= "rebel") 
    or ((player.role == "lord" or player.role == "loyalist") and (data.from.role ~= "lord" and data.from.role ~= "loyalist")))then
      room:damage({
        from = player,
        to = data.from,
        damage = 1,
        damageType = fk.NormalDamage,
        skillName = self.name,
      })
    end
  end,
}
nwei:addSkill(fange)
Fk:loadTranslationTable{
  ["tshunx__nwei"] = "年兽大魏",

  ["tshu__fange"] = "反戈",
  [":tshu__fange"] = "当你受到伤害后，你可以摸两张牌，然后若摸到的两张牌点数之差不小于你的体力值且伤害来源不为己方角色，你对伤害来源造成一点伤害",

  ["$tshu__fange"] = "前徒倒戈，攻于后以北。",
  ["~tshunx__nwei"] = " 魏兽威力，怎会倒下..",
}


local xwei = General(extension, "tshunx__xwei","wei",6)
xwei.hidden = true
local taoyuan = fk.CreateTriggerSkill{
  name = "tshu__taoyuan",
  anim_type = "defensive",
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = player:drawCards(2,self.name)
    if (Fk:getCardById(cards[1]).suit ~= Fk:getCardById(cards[2]).suit) and data.from and not data.from:isKongcheng()
    and ((player.role == "rebel" and data.from.role ~= "rebel") 
    or ((player.role == "lord" or player.role == "loyalist") and (data.from.role ~= "lord" and data.from.role ~= "loyalist"))) then
      local cards2 = data.from:getCardIds("h")
      room:obtainCard(player, table.random(cards2), false, fk.ReasonPrey)
    end
  end,
}
xwei:addSkill(taoyuan)
Fk:loadTranslationTable{
  ["tshunx__xwei"] = "夕兽大魏",

  ["tshu__taoyuan"] = "饕怨",
  [":tshu__taoyuan"] = "当你受到伤害后，你可以摸两张牌，然后若摸到的两张牌花色不同且伤害来源不为己方角色，你随机获得伤害来源一张手牌",

  ["$tshu__taoyuan"] = "恩怨因果，报应不爽。",
  ["~tshunx__xwei"] = "大荒星陨，枭兽埋名。",
}

local nshu = General(extension, "tshunx__nshu","shu",4)
nshu.hidden = true
local siyao = fk.CreateTriggerSkill{
  name = "tshu__siyao",
  anim_type = "offensive",
  events = {fk.TargetSpecified,fk.Damage},
  can_trigger = function(self, event, target, player, data)
    if event == fk.TargetSpecified then
      return target == player and player:hasSkill(self) and
      data.card and data.card.trueName == "slash" and data.firstTarget
    elseif event == fk.Damage then
      return target == player and player:hasSkill(self) and
      data.card and data.card.trueName == "slash" and data.to and data.to:isNude()
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.Damage then return true end
    local to = player.room:askForChoosePlayers(player, AimGroup:getAllTargets(data.tos), 1, 1, "#tshu__siyao-choose", self.name, true)
    if #to > 0 then
      self.cost_data = to[1]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TargetSpecified then
      player.room:damage{
        from = player,
        to = player.room:getPlayerById(self.cost_data),
        damage = 1,
        skillName = self.name,
      }
    elseif event == fk.Damage then
      local cards = data.from:getCardIds("he")
      room:moveCardTo(table.random(cards),Card.DiscardPile,player,fk.ReasonDiscard)
    end
  end,
}
local hengsao = fk.CreateTriggerSkill{
  name = "tshu__hengsao",
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    
    return target == player and player:hasSkill(self) and player.phase == Player.Play and #player:getCardIds("h") <= 6 and #player:getCardIds("h") >= 3
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player,"tshu__hengsao-phase",1)
  end,
}
local hengsao_target = fk.CreateTargetModSkill{
  name = "#tshu__hengsao_target",
  extra_target_func = function(self, player, skill, card)
    if  skill.trueName == "slash_skill" and player:getMark("tshu__hengsao-phase") > 0  then
        return 1
    end
  end,
  residue_func = function(self, player, skill, scope)
    if skill.trueName == "slash_skill" and player:getMark("tshu__hengsao-phase") > 0 then
      return 1
    end
    return 0
  end,
}
hengsao:addRelatedSkill(hengsao_target)
nshu:addSkill(siyao)
nshu:addSkill(hengsao)
Fk:loadTranslationTable{
  ["tshunx__nshu"] = "年兽大蜀",

  ["tshu__siyao"] = "撕咬",
  [":tshu__siyao"] = "当你使用【杀】指定目标后，你可以对其中一名目标造成一点伤害；当你使用的【杀】造成伤害后，受伤角色随机弃置一张牌。",
  ["tshu__hengsao"] = "横扫",
  [":tshu__hengsao"] = "锁定技，你的出牌阶段开始时，若你的手牌数在3~6之间，你本阶段可使用【杀】的次数+1,使用【杀】可指定的目标数+1",
  ["#tshu__siyao-choose"] = "撕咬：选择的其中一名角色造成一点伤害。",

  ["$tshu__siyao"] = "撕裂破碎，吞咬河山。",
  ["$tshu__hengsao"] = "横扫天下，平定四方。",
  ["~tshunx__nshu"] = "蜀兽威力，怎会倒下..",
}

local xshu = General(extension, "tshunx__xshu","shu",5)
xshu.hidden = true
local lizhan = fk.CreateTriggerSkill{
  name = "tshu__lizhan",
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  events = {fk.EventPhaseStart,fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      return target == player and player:hasSkill(self) and player.phase == Player.Play
    elseif event == fk.CardUseFinished and player:hasSkill(self) and target == player then
      local cardlist = data.card:isVirtual() and data.card.subcards or {data.card.id}
      return #player.room.logic:getEventsOfScope(GameEvent.UseCard, 3, function(e)
        local use = e.data[1]
        return use.from == player.id and use.card.trueName == "slash"
      end, Player.HistoryTurn) == 1 and data.card.trueName == "slash" and table.every(cardlist, function (id)
        return player.room:getCardArea(id) == Card.Processing
      end)
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then return true end
    local to = player.room:askForChoosePlayers(player, table.map(player.room:getOtherPlayers(player), Util.IdMapper), 1, 1, "#tshu__lizhan-card:::"..data.card:toLogString(), self.name, true)
    if #to > 0 then
      self.cost_data = to[1]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      local cards = player.room:getCardsFromPileByRule("slash", 2,"disicardPile")
      if #cards > 0 then
        player.room:obtainCard(player, cards[1], true, fk.ReasonJustMove)
      end
      if #cards > 1 then
        player.room:obtainCard(player, cards[2], true, fk.ReasonJustMove)
      end
    elseif event == fk.CardUseFinished then
      room:moveCards({
        to = self.cost_data,
        toArea = Card.PlayerHand,
        ids = room:getSubcardsByRule(data.card, { Card.Processing }),
        proposer = player.id,
        moveReason = fk.ReasonGive,
      })
    end
  end,
}
xshu:addSkill("os_ex__paoxiao")
xshu:addSkill(lizhan)
Fk:loadTranslationTable{
  ["tshunx__xshu"] = "夕兽大蜀",

  ["tshu__lizhan"] = "励战",
  [":tshu__lizhan"] = "锁定技，你的出牌阶段开始时，你从弃牌堆获得两张【杀】；你每回合第一张【杀】结算后，你可以将其交给一名其他角色。",
  ["#tshu__lizhan-card"] = "励战：你可以将 %arg 交给一名其他角色。",

  ["$tshu__lizhan"] = "身经百战，大蜀万丈。",
  ["$os_ex__paoxiao_tshunx__xshu"] = "气惊六合。虎啸八方！",
  ["~tshunx__xshu"] = "折翼沙场，大业一梦...",
}

local nwu = General(extension, "tshunx__nwu","wu",5,5,General.Female)
nwu.hidden = true
local zhuyan = fk.CreateTriggerSkill{
  name = "tshu__zhuyan",
  anim_type = "drawcard",
  frequency = Skill.Compulsory,
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Draw
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local ids = table.random(room.draw_pile,4)
      if #ids > 0 then
        for _, cid in ipairs(ids) do
          room:obtainCard(player, cid, false, fk.ReasonPrey)
        end
      end
    return true
  end,
}
nwu:addSkill(zhuyan)
nwu:addSkill("xiaoji")
Fk:loadTranslationTable{
  ["tshunx__nwu"] = "年兽大吴",

  ["tshu__zhuyan"] = "朱颜",
  [":tshu__zhuyan"] = "锁定技，摸牌阶段，你改为随机获得牌堆中的四张牌。",

  ["$tshu__zhuyan"] = "朱颜佳色，倾倒众生。",
  ["$xiaoji_tshunx__nwu"] = "这些装备，还是给我更好。",
  ["~tshunx__nwu"] = "吴兽威力，怎会倒下..",
}

local xwu = General(extension, "tshunx__xwu","wu",4,4,General.Female)
xwu.hidden = true
xwu:addSkill("mingzhe")
xwu:addSkill("ol_ex__tianxiang")
Fk:loadTranslationTable{
  ["tshunx__xwu"] = "夕兽大吴",

  ["$mingzhe_tshunx__xwu"] = "赤诚之心，天地可鉴。",
  ["$ol_ex__tianxiang_tshunx__xwu"] = "携红为秉，否极泰来。",
  ["~tshunx__xwu"] = "爆竹惊魂，落花流水。",
}

local nqun = General(extension, "tshunx__nqun","qun",5)
nqun.hidden = true
local qunxiang = fk.CreateTriggerSkill{
  name = "tshu__qunxiang",
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
      return player:hasSkill(self) and target == player and (player.phase == Player.Start or player.phase == Player.Finish) and (not player:prohibitUse(Fk:cloneCard("archery_attack")) or not player:prohibitUse(Fk:cloneCard("savage_assault")) )
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choices = {}
    if not player:prohibitUse(Fk:cloneCard("archery_attack")) then
      table.insertIfNeed(choices,"archery_attack")
    end
    if not player:prohibitUse(Fk:cloneCard("savage_assault")) then
      table.insertIfNeed(choices,"savage_assault")
    end
    local choice = room:askForChoice(player,choices,self.name,nil,false,{"archery_attack","savage_assault"})
    U.askForUseVirtualCard(room, player,choice, nil, self.name)
  end,
}
local tanshi = fk.CreateTriggerSkill{
  name = "tshu__tanshi",
  anim_type = "drawcard",
  events = {fk.Damage},
  can_trigger = function(self, event, target, player, data)
      return player:hasSkill(self) and target == player
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local judge = {
      who = player,
      reason = self.name,
      pattern = ".|.|club,spade",
    }
    room:judge(judge)
    if judge.card.color == Card.Black and not player.dead then
      if player:isWounded() then
        room:recover({
          who = player,
          num = 1,
          recoverBy = player,
          skillName = self.name
          })
      else
        player:drawCards(1,self.name)
      end
    end
  end,
}
nqun:addSkill(qunxiang)
nqun:addSkill(tanshi)
Fk:loadTranslationTable{
  ["tshunx__nqun"] = "年兽大群",

  ["tshu__qunxiang"] = "群响",
  [":tshu__qunxiang"] = "锁定技，你的准备阶段或结束阶段，你可以视为使用一张【万箭齐发】或【南蛮入侵】。",
  ["tshu__tanshi"] = "贪食",
  [":tshu__tanshi"] = "当你造成伤害后，你可以进行判定，若判定结果为黑色，你回复一点体力（若满血则改为摸一张牌）。",

  ["$tshu__qunxiang"] = "合群之力，响动四海。",
  ["$tshu__tanshi"] = "这么多好吃的，都是我的了。",
  ["~tshunx__nqun"] = "群兽之力，怎会倒下..",

}

local xqun = General(extension, "tshunx__xqun","qun",5)
xqun.hidden = true
local shouxi = fk.CreateTriggerSkill{
  name = "tshu__shouxi",
  anim_type = "control",
  events = {fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
      return player:hasSkill(self) and target == player and data.card.type == Card.TypeTrick and data.card.color == Card.Black
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local judge = {
      who = player,
      reason = self.name,
      pattern = ".|.|heart,diamond,club,spade",
    }
    room:judge(judge)
    if judge.card.color == Card.Black  then
      local tar = {}
      for _, p in ipairs(room.alive_players) do
        if (player.role == "rebel" and p.role ~= "rebel") 
    or ((player.role == "lord" or player.role == "loyalist") and (p.role ~= "lord" and p.role ~= "loyalist")) then
          table.insertIfNeed(tar,p)
        end
      end
      table.shuffle(tar)
      if #tar > 0 then
        player.room:damage{
          from = player,
          to = tar[1],
          damage = 1,
          skillName = self.name,
        }
      end
    elseif judge.card.color == Card.Red and not player.dead then
      room:obtainCard(player, judge.card.id, false, fk.ReasonPrey)
    end
  end,
}
xqun:addSkill("juxiang")
xqun:addSkill(shouxi)
Fk:loadTranslationTable{
  ["tshunx__xqun"] = "夕兽大群",

  ["tshu__shouxi"] = "兽袭",
  [":tshu__shouxi"] = "当你使用黑色锦囊牌时，你可以进行判定：若为黑色，你随机对一名敌方角色造成一点伤害；若为红色，你获得判定牌。",

  ["$tshu__shouxi"] = "磨牙砺爪，一击而就。",
  ["$juxiang_tshunx__xqun"] = "山神打鼓，百兽狂奔。",
  ["~tshunx__xqun"] = "茕茕困兽，东走西顾。",

}



local pucong = General(extension, "tshunx__pucong","qun",3)
pucong.hidden = true
local indulgenceSkill = fk.CreateActiveSkill{
  name = "tshu__indulgence_skill",
  mod_target_filter = function(self, to_select, selected, user, card)
    return user ~= to_select
  end,
  target_filter = function(self, to_select, selected, _, card)
    return #selected == 0 and self:modTargetFilter(to_select, selected, Self.id, card)
  end,
  target_num = 1,
  on_effect = function(self, room, effect)
    local to = room:getPlayerById(effect.to)
    local judge = {
      who = to,
      reason = "indulgence",
      pattern = ".|.|heart",
    }
    room:judge(judge)
    local result = judge.card
    if result.suit == Card.Heart then
      to:skip(Player.Play)
    end
    self:onNullified(room, effect)
  end,
  on_nullified = function(self, room, effect)
    room:moveCards{
      ids = room:getSubcardsByRule(effect.card, { Card.Processing }),
      toArea = Card.DiscardPile,
      moveReason = fk.ReasonUse
    }
  end,
}
Fk:addSkill(indulgenceSkill)
local supplyShortageSkill = fk.CreateActiveSkill{
  name = "tshu__supply_shortage_skill",
  distance_limit = 1,
  mod_target_filter = function(self, to_select, _, user, card, distance_limited)
    local player = Fk:currentRoom():getPlayerById(to_select)
    local from = Fk:currentRoom():getPlayerById(user)
    return from ~= player and not (distance_limited and not self:withinDistanceLimit(from, false, card, player))
  end,
  target_filter = function(self, to_select, selected, _, card, extra_data)
    local count_distances = not (extra_data and extra_data.bypass_distances)
    return #selected == 0 and self:modTargetFilter(to_select, selected, Self.id, card, count_distances)
  end,
  target_num = 1,
  on_effect = function(self, room, effect)
    local to = room:getPlayerById(effect.to)
    local judge = {
      who = to,
      reason = "supply_shortage",
      pattern = ".|.|club",
    }
    room:judge(judge)
    local result = judge.card
    if result.suit == Card.Club then
      to:skip(Player.Draw)
    end
    self:onNullified(room, effect)
  end,
  on_nullified = function(self, room, effect)
    room:moveCards{
      ids = room:getSubcardsByRule(effect.card, { Card.Processing }),
      toArea = Card.DiscardPile,
      moveReason = fk.ReasonUse
    }
  end,
}
Fk:addSkill(supplyShortageSkill)
local xiongshou = fk.CreateTriggerSkill{
  name = "tshu__xiongshou",
  frequency = Skill.Compulsory,
  events = {fk.CardEffecting,fk.DrawNCards,fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if event == fk.CardEffecting then
      return player:hasSkill(self) and target == player and (data.card.trueName == "indulgence" or data.card.trueName == "supply_shortage")
    elseif event == fk.DrawNCards then
      return target == player and player:hasSkill(self)
    elseif event == fk.EventPhaseStart then
      return target == player and player:hasSkill(self) and player.phase == Player.Finish
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.DrawNCards then
      data.n = data.n + 1
    elseif event == fk.EventPhaseStart then
      if not player.dead then
        player:drawCards(1,self.name)
      end
    elseif event == fk.CardEffecting then
      local card = data.card:clone()
      local c = table.simpleClone(data.card)
      for k, v in pairs(c) do
        card[k] = v
      end
      if data.card.trueName == "indulgence" then
        local ogri_skill = Fk.skills["indulgence_skill"]
        card.skill = (card.skill == ogri_skill) and indulgenceSkill or ogri_skill
        data.card = card
      elseif data.card.trueName == "supply_shortage" then
        local ogri_skill = Fk.skills["supply_shortage_skill"]
        card.skill = (card.skill == ogri_skill) and supplyShortageSkill or ogri_skill
        data.card = card
      end
    end
  end,
}
local xisheng = fk.CreateTriggerSkill{
  name = "tshu__xisheng",
  anim_type = "support",
  events = {fk.Death},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self.name, false, true)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room.alive_players) do
      if (player.role == "rebel" and p.role == "rebel") 
      or ((player.role == "lord" or player.role == "loyalist") and (p.role == "lord" or p.role == "loyalist")) then
        if math.random(1,2) == 1 and p:isWounded() then
          room:recover({
            who = p,
            num = 1,
            recoverBy = player,
            skillName = self.name
            })
          elseif not p.dead then
          p:drawCards(1,self.name)
        end
      end
    end
  end,
}
pucong:addSkill(xiongshou)
pucong:addSkill(xisheng)
Fk:loadTranslationTable{
  ["tshunx__pucong"] = "年兽仆从",

  ["tshu__xiongshou"] = "凶兽",
  [":tshu__xiongshou"] = "锁定技，你判定区的【乐不思蜀】或【兵粮寸断】判定效结果反转；摸牌阶段你多摸一张牌，结束阶段你摸一张牌。",
  ["tshu__xisheng"] = "牺牲",
  [":tshu__xisheng"] = "锁定技，你死亡时，所有友方角色随机摸一张牌或回复一点体力。",

  ["$tshu__xiongshou"] = "这些，别想阻止我！",
  ["$tshu__xisheng"] = "请不要辜负所托。",
  ["~tshunx__pucong"] = "下次，你就没用这样的好运了..",

}
return extension